<?php
/**
 * 银行模型
 * User: wuhao
 * Date: 2017/5/5
 * Time: 15:03
 */
namespace app\backend\model;

use think\Model;

class Bank extends Model
{
    private $bankTable = 'z_bank';
    private $bankCodeTable = 'z_bank_code';
    private $subbranchTable = 'z_bank_subbranch';
    private $bankAreaTable = 'z_bank_area';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取银行代码
     * @date   2017-05-05
     * @author wuhao
     *
     * @return
     */
    public function bankCode()
    {
        return $this->setTable($this->bankCodeTable)->select();
    }


    /**
     * 获取银行信息
     * @author lanlc
     * @date   2017-07-08
     *
     * @param  $cityId 城市代码 1620 => 大同市
     * @param  $bankId 银行代码 504 => 恒生银行
     * @param  $searchName 模糊搜索 词
     * @param  $page 第几页
     * @param  $size 条数
     * @return
     */
    public function getSubbranch($cityId, $bankId, $searchName = '', $page = 1, $size = 500)
    {
        $cityId     = intval($cityId);
        $bankId     = intval($bankId);
        $searchName = strval($searchName);
        $page       = intval($page);
        $size       = intval($size);

        if (!$cityId || !$bankId) {
            return false;
        }

        $where = [
            'city'    => $cityId,
            'bank_id' => $bankId
        ];

        if ($searchName !== '') {
            $where['name'] = ['like', "%{$searchName}%"];
        }
        $offSet = ($page-1)*$size;

        $field = 'code,name,phone';

        $count = $this->setTable($this->subbranchTable)->field($field)->where($where)->count();
        $list  = $this->setTable($this->subbranchTable)->field($field)->where($where)->limit($offSet, $size)->select();

        return $list === false ? false : ['count' => $count, 'list' => $list];
    }

    /**
     * 获取省份
     * @date   2016-01-10
     * @author Lanlc
     *
     * @return
     */
    public function getBankProvince()
    {
        return $this->setTable($this->bankAreaTable)->where(['parent_id' => 0])->select();
    }

    /**
     * 获取市
     * @date   2016-01-10
     * @author Lanlc
     *
     * @return
     */
    public function getBankArea($provinceId)
    {
        if (!$provinceId) {
            return false;
        }
        return $this->setTable($this->bankAreaTable)->where(['parent_id' => $provinceId])->select();
    }

    /**
     * 新增银行卡信息
     * @date   2017-05-05
     * @author wuhao
     *
     * @param  array $bankData  数据
     *
     * @return
     */
    public function addBankCard($bankData)
    {
        if (!$bankData) {
            return false;
        }
        return $this->setTable($this->bankTable)->insertGetId($bankData);
    }

    /**
 * 新增银行卡信息
 * @date   2017-05-05
 * @author wuhao
 *
 * @param  string  $bankCard  银行卡号
 * @param  int     $status  卡状态 0=已删除 1=正常
 * @return array
 */
    public function getBankCard($bankCard, $status = 1)
    {
        if (!$bankCard || !in_array($status, [0,1])) {
            return false;
        }
        $where = ['bank' => $bankCard, 'status' => $status];
        $res   = $this->setTable($this->bankTable)->where($where)->find();
        return $res ?: [];
    }

    /**
     * 判断是否已经绑定过银行卡
     * @date   2017-05-05
     * @author wuhao
     *
     * @param  int|array    $memberId  用户id
     * @param  int          $status  卡状态 0=已删除 1=正常
     * @return array
     */
    public function getBankCardByMid($memberId, $status = 1)
    {
        if (!$memberId  || !in_array($status, [0,1])) {
            return false;
        }
        if (is_array($memberId)) {
            $where = ['mid' => ['in', $memberId], 'status' => $status];
            $res   = $this->setTable($this->bankTable)->where($where)->select();
        } else {
            $where = ['mid' => $memberId, 'status' => $status];
            $res   = $this->setTable($this->bankTable)->where($where)->find();
        }
        return $res ?: [];
    }

    /**
     * 获取个人的银行卡信息
     * @date   2017-05-08
     * @author wuhao
     *
     * @param  int  $mid  用户id
     * @param  int  $status 卡状态 0=未删除 1=正常
     * @return array
     */
    public function getBankInfoByMid($mid, $status = 1)
    {
        if (!$mid || !in_array($status, [0, 1])) {
            return false;
        }
        $where = ['mid' => $mid, 'status' => $status];
        $res = $this->setTable($this->bankTable)->where($where)->select();
        return $res ?: [];
    }

    /**
     * 根据银行编码获取其信息
     * @date   2017-05-08
     * @author wuhao
     *
     * @param  string|array  $code  银行代码
     * @param  string        $field 获取的字段
     * @return array
     */
    public function getBankCodeInfoByCode($code, $field = '*')
    {
        if (!$code || !$field) {
            return false;
        }
        $this->setTable($this->bankCodeTable)->field($field);
        if (is_array($code)) {
            $where = ['code' => ['in', $code]];
            $res = $this->where($where)->select();
        } else {
            $where = ['code' => $code];
            $res = $this->where($where)->find();
        }
        return $res ?: [];
    }

    /**
     * 删除绑定银行卡信息
     * @date   2017-05-08
     * @author wuhao
     *
     * @param  string   $bank  银行卡号
     * @param  int      $memberId 用户id
     * @return array
     */
    public function deleteBankCard($bank, $memberId)
    {
        if (!$bank ||!$memberId) {
            return false;
        }
        $where = ['bank' => $bank, 'mid' => $memberId, 'status' => 1];
        $data  = ['status' => 0, 'update_time' => time()];
        return $this->setTable($this->bankTable)->where($where)->update($data);
    }
}